package tienda.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import tienda.exception.DAOExcepcion;
import tienda.modelo.Persona;
import tienda.util.ConexionBD;


public class PersonaDAO extends BaseDAO {

	public Persona insertar(Persona vo) throws DAOExcepcion {
		String query = "insert into persona(nombres, paterno, materno, sexo, email, fecnacimiento, codUsuario, contrasena, " +
				"TipoPersona_idTipoPersona, celular, documento, nroDocumento) " +
				"values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
			
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getNombres());
			stmt.setString(2, vo.getPaterno());
			stmt.setString(3, vo.getMaterno());
			stmt.setString(4, vo.getSexo());
			stmt.setString(5, vo.getEmail());
			stmt.setString(6, vo.getFecnacimiento());
			stmt.setString(7, vo.getCodUsuario());
			stmt.setString(8, vo.getContrasena());
			stmt.setInt(9, vo.getTipoPersona_idTipoPersona());
			stmt.setString(10, vo.getCelular());
			stmt.setString(11, vo.getDocumento());
			stmt.setString(12, vo.getNroDocumento());
				
								
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
			// Obtener el ultimo id
			int id = 0;
			query = "select last_insert_id()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				id = rs.getInt(1);
			}
			vo.setIdPersona(id);

		} catch (SQLException e) {
			//System.err.println(e.getSQLState());
			throw new DAOExcepcion(e.getSQLState());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	public Persona obtener(int idPersona) throws DAOExcepcion {
		Persona vo = new Persona();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "select nroDocumento, codUsuario, email from persona where idPersona=?";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idPersona);
			rs = stmt.executeQuery();
			
			if (rs.next()) {
				vo.setNroDocumento(rs.getString(1));
				vo.setCodUsuario(rs.getString(2));
				vo.setEmail(rs.getString(3));
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}
	
	public Collection<Persona> buscarPorNombre(String nombre)
			throws DAOExcepcion {
		String query = "select idPersona, nroDocumento, email from persoma where nombre like ?";
		Collection<Persona> lista = new ArrayList<Persona>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, "%" + nombre + "%");
			rs = stmt.executeQuery();
			while (rs.next()) {
				Persona vo = new Persona();
				vo.setIdPersona(rs.getInt("idPersona"));
				vo.setNroDocumento(rs.getString("nroDocumento"));
				vo.setEmail(rs.getString("email"));
		
				lista.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		System.out.println(lista.size());
		return lista;
	}

	
	
	
//buscar para inicio de sesion
	
	public Collection<Persona> buscardatossesion(String usuario, String pass)
			throws DAOExcepcion {
		String query = "select idPersona, nombres,paterno,materno from persona where codUsuario=? and contrasena= ?";

		Collection<Persona> lista = new ArrayList<Persona>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1,usuario);
			stmt.setString(2,pass);
			rs = stmt.executeQuery();

			while (rs.next()) {
				Persona vo = new Persona();
				vo.setIdPersona(rs.getInt("idPersona"));
				vo.setNombres(rs.getString("nombres"));
				vo.setPaterno(rs.getString("paterno"));
				vo.setMaterno(rs.getString("materno"));
		     	lista.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		System.out.println(lista.size());
		return lista;
	}
	
	
	
	
	
	
	
	
	
	
	
}